﻿


using AutoMapper;
using B.S.Unit.Domain;
using B.S.Unit.ErrorCode;
using B.S.Unit.Infacet;
using B.S.Unit.Write.API.Applications.Command;
using MediatR;

using MediatR;

namespace B.S.Business.Write.API.Applications.CommandHandler
{

    public class UnitDelCommandHandler : IRequestHandler<UnitDelCommand, UnitResult<object>>
    {
        private readonly IBaseRepository<UnitApp> UnitApp;
        private readonly IMapper mapper;

        public UnitDelCommandHandler(IBaseRepository<UnitApp> UnitApp, IMapper mapper)
        {
            this.UnitApp = UnitApp;
            this.mapper = mapper;
        }
        public Task<UnitResult<object>> Handle(UnitDelCommand request, CancellationToken cancellationToken)
        {
            UnitResult<object> result = new UnitResult<object>();

            var obj = UnitApp.GetAll().FirstOrDefault(x => x.UnitAppId == request.UnitAppId);
            obj.IsDeleted = true;
            var res = UnitApp.Update(obj);
            if (res == 1)
            {
                result.Code = UnitCode.删除成功;
                result.Msg = UnitCode.删除成功.ToString();
                result.Data = res;
            }
            else
            {
                result.Code = UnitCode.删除失败;
                result.Msg = UnitCode.删除失败.ToString();
                result.Data = res;
            }
            return Task.FromResult(result);
        }
    }
}
